import { getToken, setToken, removeToken } from "@/utils/auth";
import { login } from "@/api/user";
const state = {
  token: getToken(),
};
const mutations = {
  // 设置vuex 的token
  setToken(state, token) {
    state.token = token;

    // 更新本地的token
    setToken(token);
  },

  // 删除vuex的token
  removeToken(state, token) {
    state.token = token;
    removeToken();
  },
};

const actions = {
  // 登录的方法
  async login(context, data) {
    const res = await login(data);
    context.commit("setToken", res.token);
  },
  // 退出登录的方法
  logout(context) {
    context.commit("removeToken");
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
